//
//  YYTextController.swift
//  YYProject
//
//  Created by 陆文宇 on 2017/9/1.
//  Copyright © 2017年 陆文宇. All rights reserved.
//

import UIKit

class YYTextController: YYController, UITextViewDelegate {
    
    var saveClosure: ((String) -> Void)!
    
    var canEmpty: Bool = false
    var textCountLimit = 0
    var placeholder: String!
    var text: String = ""
    var doneTitle: String = "保存"
    
    @IBOutlet weak var placeholderLabel: UILabel!
    @IBOutlet weak var textView: UITextView!
    @IBOutlet weak var textCountLabel: UILabel!
    @IBOutlet weak var textViewBottom: NSLayoutConstraint!
    
    override func yy_configNaviItem(_ naviItem: UINavigationItem) {
        super.yy_configNaviItem(naviItem)
        
        let saveItem = UIBarButtonItem(title: doneTitle, style: .done, target: self, action: #selector(save(_:)))
        naviItem.rightBarButtonItem = saveItem
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        textCountLabel.text = "\(text.count)/\(textCountLimit)"
        
        if text.isEmpty {
            placeholderLabel.text = placeholder
        } else {
            textView.text = text
            placeholderLabel.isHidden = true
        }

        textView.becomeFirstResponder()
        NotificationCenter.default.addObserver(forName: Notification.Name.UIKeyboardWillShow, object: nil, queue: nil) { (notification) in
            if let userInfo = notification.userInfo,
                let value = userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue {
                self.textViewBottom.constant = value.cgRectValue.size.height
            }
        }
    }
    
    override func yy_shouldPop() -> Bool {
        if textView.text != text {
            UIAlertController.yy_showAlert(title: "内容尚未保存", message: "是否继续退出", confirmAction: { 
                self.yy_pop()
            })
            return false
        } else {
            return true
        }
    }
    
    @objc func save(_ sender: UIBarButtonItem) {
        if !canEmpty && textView.text.isEmpty {
            UIAlertController.yy_showAlert(title: "内容不能为空")
            return
        }
        
        if textView.text.count > textCountLimit {
            UIAlertController.yy_showAlert(title: "字数超过限制")
            return
        }
        
        yy_pop()
        saveClosure(textView.text)
    }
    
    func textViewDidChange(_ textView: UITextView) {
        placeholderLabel.isHidden = !textView.text.isEmpty
        
        textCountLabel.text = "\(textView.text.count)/\(textCountLimit)"
        
        if textView.text.count > textCountLimit {
            textCountLabel.textColor = .red
        } else {
            textCountLabel.textColor = .darkGray
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
